package com.theexceptions.todo.view;

import com.theexceptions.todo.controller.TasksController;
import com.theexceptions.todo.model.Task;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * An activity that display's the current user's task with any/all applied
 * filters. Corresponds to the view: tasks.xml.
 * 
 * @author Justian Meyer
 */
public class TasksActivity extends ListActivity {

	private TaskArrayAdapter adapter;

	@Override
	public void onCreate(Bundle info) {
		super.onCreate(info);

		// Retrieve an arrayAdapter with user's filtered items
		adapter = new TaskArrayAdapter(this, R.layout.tasks, TasksController.getCurrentUserTasks());
		setListAdapter(adapter);

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		// Configure actions when list item is clicked
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				// Toggle task and display the change
				Task task = (Task) parent.getItemAtPosition(position);
				TasksController.toggleTaskCompleted(task);
				adapter.notifyDataSetChanged();
			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.tasks_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.filter:
			Toast.makeText(this, "[unimplemented]", Toast.LENGTH_LONG).show();
			break;
		case R.id.viewmap:
			Toast.makeText(this, "[unimplemented]", Toast.LENGTH_LONG).show();
			break;
		}
		return true;
	}

	/**
	 * An custom adapter used when displaying the list of user tasks.
	 * 
	 * @author JD Reddaway
	 */
	private class TaskArrayAdapter extends ArrayAdapter<Task> {

		/**
		 * Constructs a TaskArrayAdapter
		 * 
		 * @param context
		 * @param textViewResourceId
		 * @param objects
		 */
		public TaskArrayAdapter(Context context, int textViewResourceId, Task[] objects) {
			super(context, textViewResourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			Task t = this.getItem(position);

			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.tasks, null);
			}

			if (t != null) {
				TextView tv = (TextView) v;

				tv.setTextColor(t.isCompleted() ? Color.DKGRAY : Color.WHITE);
				tv.setText(t.toString());
			}

			return v;
		}
	}
}
